package cn.tedu.tickets;



/*需求:设计多线程模型,4个售票窗口共计100张
* 本方案使用多线程编程方案2,实现Runnable的方式来完成*/
public class TestRunnable {
    public static void main(String[] args) {
        TicketRannable target = new TicketRannable();

        Thread t1 = new Thread(target);
        Thread t2 = new Thread(target);
        Thread t3 = new Thread(target);
        Thread t4 = new Thread(target);
        t1.start();
        t2.start();
        t3.start();
        t4.start();

    }

}
class TicketRannable implements Runnable{
   /*由于自定义对象,作为唯一的业务对象,只new了一次
   * 所以发布的任务,也就是票数天然的会被所有线程对象共享,无需设置为静态,也只有一份*/

      int tickets=100;
    @Override
    public void run() {
        while (true){
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"="+tickets--);
            if (tickets<=0) break;
        }
    }
}